﻿using System;
using System.Web;
using newtelligence.DasBlog.Runtime;

namespace newtelligence.DasBlog.Web.Core
{
    public static class Seo
    {
        private const string CanonicalLinkPattern = "<link rel=\"canonical\" href=\"{0}\" />\r\n";
        private const string MetaDescriptionTagPattern = "<meta name=\"description\" content=\"{0}\" />\r\n";
        private const string MetaKeywordTagPattern = "<meta name=\"keywords\" content=\"{0}\" />\r\n";
        private const string MetaNoindexFollowPattern = "<meta name=\"robots\" content=\"noindex,follow\" />\r\n";

        public static string CreateSeoMetaInformation(EntryCollection weblogEntries, IBlogDataService dataService)
        {
            string metaTags = "\r\n";
            string currentUrl = HttpContext.Current.Request.Url.AbsoluteUri;

            if (currentUrl.IndexOf("categoryview.aspx", StringComparison.OrdinalIgnoreCase) > -1 || currentUrl.IndexOf("default.aspx?month=", StringComparison.OrdinalIgnoreCase) > -1)
            {
                metaTags += MetaNoindexFollowPattern;
            }

            else if (currentUrl.IndexOf("permalink.aspx", StringComparison.OrdinalIgnoreCase) > -1)
            {
                if (weblogEntries.Count >= 1)
                {
                    Entry entry = weblogEntries[0];
                    metaTags = GetMetaTags(metaTags, entry);
                }
            }

            else if (currentUrl.IndexOf("commentview.aspx", StringComparison.OrdinalIgnoreCase) > -1 && !string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["guid"]))
            {
                Entry entry = dataService.GetEntry(HttpContext.Current.Request.QueryString["guid"]);
                if (entry != null)
                {
                    metaTags = GetMetaTags(metaTags, entry);
                }
            }

            else if (currentUrl.IndexOf("default.aspx", StringComparison.OrdinalIgnoreCase) > -1)
            {
                var smt = new SeoMetaTags();
                smt = smt.GetMetaTags();
                if (smt != null)
                {
                    if (!string.IsNullOrEmpty(smt.MetaDescription))
                    {
                        metaTags += string.Format(MetaDescriptionTagPattern, smt.MetaDescription);
                    }

                    if (!string.IsNullOrEmpty(smt.MetaKeywords))
                    {
                        metaTags += string.Format(MetaKeywordTagPattern, smt.MetaKeywords);
                    }
                }
                metaTags += string.Format(CanonicalLinkPattern, SiteUtilities.GetBaseUrl());
            }
            return metaTags;
        }

        private static string GetMetaTags(string metaTags, Entry entry)
        {
            // Canonical Tag
            metaTags += string.Format(CanonicalLinkPattern, SiteUtilities.GetPermaLinkUrl(entry));

            // Meta Description
            string blogPostDescription;

            if (string.IsNullOrEmpty(entry.Description) || entry.Description.Length < 20)
            {
                blogPostDescription = entry.Content;
            }
            else
            {
                blogPostDescription = entry.Description;
            }

            blogPostDescription = HttpUtility.HtmlDecode(blogPostDescription);
            blogPostDescription = blogPostDescription.RemoveLineBreaks();
            blogPostDescription = blogPostDescription.StripHtml();
            blogPostDescription = blogPostDescription.RemoveDoubleSpaceCharacters();
            blogPostDescription = blogPostDescription.Trim();
            blogPostDescription = blogPostDescription.CutLongString(160);
            blogPostDescription = blogPostDescription.RemoveQuotationMarks();

            metaTags += string.Format(MetaDescriptionTagPattern, blogPostDescription);

            // Meta Keywords
            if (!string.IsNullOrEmpty(entry.Categories))
            {
                metaTags += string.Format(MetaKeywordTagPattern, entry.Categories.Replace(';', ' '));
            }

            return metaTags;
        }
    }
}